using micromsg;
using MicroMsg.Common.Timer;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Scene.Voice
{
	public class NetSceneDownloadVoice : NetSceneBaseEx<DownloadVoiceRequest, DownloadVoiceResponse, DownloadVoiceRequest.Builder>
	{
		private const string TAG = "NetSceneDownloadVoice";
		private const int MAX_TIMES_DOWNLOAD_BLOCK = 300;
		private DownloadVoiceContext mVoiceContext;
		private TimerObject mTimerObject;
		public event onSceneDownloadFinishedDelegate mOnSceneFinished;
		public void doScene(DownloadVoiceContext voiceContext)
		{
			if (voiceContext == null)
			{
				Log.smethod_5("NetSceneDownloadVoice", "voiceContext is null");
				return;
			}
			if (voiceContext.isRunning())
			{
				Log.smethod_5("NetSceneDownloadVoice", "doScene recving now, status = " + voiceContext.mStatus);
				return;
			}
			Log.smethod_4("NetSceneDownloadVoice", "NetSceneDownloadVoice do scene,download a msg. mMsgSvrID = " + voiceContext.mMsgSvrID);
			this.mVoiceContext = voiceContext;
			this.mVoiceContext.mStatus = 1;
			DownloadVoiceStorage.updateVoiceMsgStatus(voiceContext.strTalker, voiceContext.mMsgSvrID, MsgUIStatus.Processing);
			if (this.mTimerObject == null)
			{
				this.mTimerObject = TimerService.addTimer(1, new EventHandler(NetSceneDownloadVoice.onTimerHandler), 1, -1, new TimerEventArgs(this));
				this.mTimerObject.start();
			}
			NetSceneDownloadVoice.onTimerHandler(null, new TimerEventArgs(this));
		}
		public static void onTimerHandler(object sender, EventArgs e)
		{
			NetSceneDownloadVoice netSceneDownloadVoice = TimerEventArgs.getObject(e) as NetSceneDownloadVoice;
			if (netSceneDownloadVoice == null)
			{
				Log.smethod_1("NetSceneDownloadVoice", "onTimerHandler, invalid timer args");
				return;
			}
			if (netSceneDownloadVoice.mVoiceContext == null)
			{
				Log.smethod_5("NetSceneDownloadVoice", "scene.mVoiceContext is null,doScene already finished");
				return;
			}
			if (netSceneDownloadVoice.mVoiceContext.isBlockRunning())
			{
				Log.smethod_5("NetSceneDownloadVoice", "doScene recving msg block now status=" + netSceneDownloadVoice.mVoiceContext.mStatus);
				return;
			}
			Log.smethod_4("NetSceneDownloadVoice", "doScene start download msg block now status=" + netSceneDownloadVoice.mVoiceContext.mStatus);
			MsgTrans currentVoiceBlock = netSceneDownloadVoice.mVoiceContext.GetCurrentVoiceBlock();
			if (currentVoiceBlock == null)
			{
				if (netSceneDownloadVoice.mTimerObject.FireCount > 300)
				{
					Log.smethod_1("NetSceneDownloadVoice", "the block timer reached the max times = " + 300);
					netSceneDownloadVoice.doSceneFinished(5);
					netSceneDownloadVoice.cancel();
				}
				return;
			}
			netSceneDownloadVoice.doSceneBlock(currentVoiceBlock);
		}
		public void doSceneFinished(int status)
		{
			Log.smethod_4("NetSceneDownloadVoice", "netscene do finished. status=" + status);
			this.mVoiceContext.mStatus = status;
			DownloadVoiceStorage.updateDownloadVoiceContext(new MsgTrans
			{
				nMsgSvrID = this.mVoiceContext.mMsgSvrID,
				nStatus = this.mVoiceContext.mStatus
			});
			if (this.mTimerObject != null)
			{
				this.mTimerObject.stop();
				this.mTimerObject = null;
			}
			if (this.mOnSceneFinished != null)
			{
				this.mOnSceneFinished(this.mVoiceContext);
			}
			this.mVoiceContext = null;
		}
		private bool doSceneBlock(MsgTrans voiceinfo)
		{
			Log.smethod_4("NetSceneDownloadVoice", string.Concat(new object[]
			{
				"NetSceneDownloadVoice do scene,download a msg block, endflag=",
				voiceinfo.nEndFlag,
				" svrMsgID=",
				voiceinfo.nMsgSvrID
			}));
			this.mVoiceContext.mStatus = 2;
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(20);
			this.mBuilder.MsgId = (uint)this.mVoiceContext.mMsgSvrID;
			this.mBuilder.Offset = (uint)this.mVoiceContext.mOffset;
			voiceinfo.nTransDataLen = this.mVoiceContext.mOffset;
			uint num = (uint)(voiceinfo.nTotalDataLen - voiceinfo.nTransDataLen);
			this.mBuilder.Length = ((num > 65536u) ? 65536u : num);
			if (voiceinfo.strClientMsgId != null && voiceinfo.strClientMsgId.get_Length() > 0)
			{
				this.mBuilder.ClientMsgId = voiceinfo.strClientMsgId;
			}
			this.mSessionPack.mCmdID = 20;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(DownloadVoiceRequest request, DownloadVoiceResponse response)
		{
			Log.smethod_1("NetSceneDownloadVoice", "NetSceneDownloadVoice do scene failed");
			this.doSceneFinished(5);
		}
		protected override void onSuccess(DownloadVoiceRequest request, DownloadVoiceResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneDownloadVoice", "NetSceneDownloadVoice do scene failed, ret = " + ret);
				this.doSceneFinished(5);
				return;
			}
			if (response.CancelFlag != 0u)
			{
				Log.smethod_5("NetSceneDownloadVoice", "NetSceneDownloadVoice do scene failed, voice has been canceled, response.CancelFlag " + response.CancelFlag);
				this.doSceneFinished(5);
				return;
			}
			if (response.EndFlag != 0u && response.Data.Buffer.Length <= 0)
			{
				Log.smethod_1("NetSceneDownloadVoice", "receive err packet");
				this.doSceneFinished(5);
				return;
			}
			MsgTrans currentVoiceBlock = this.mVoiceContext.GetCurrentVoiceBlock();
			if ((long)currentVoiceBlock.nMsgSvrID != (long)((ulong)response.MsgId))
			{
				Log.smethod_1("NetSceneDownloadVoice", "received invalid msg");
				this.doSceneFinished(5);
				return;
			}
			if (response.Data.Buffer.Length == 0 && currentVoiceBlock.nEndFlag == 0)
			{
				Log.smethod_1("NetSceneDownloadVoice", "errmsg, the voice data is empty,but endflag is 0 received a msg complete");
				this.doSceneFinished(5);
				return;
			}
			if (!StorageMgr.msgVoice.saveVoiceBlockData(currentVoiceBlock, response.Data.Buffer.ToByteArray(), null))
			{
				Log.smethod_1("NetSceneDownloadVoice", "errmsg, save block failed");
				this.doSceneFinished(5);
				return;
			}
			this.mVoiceContext.mOffset = this.mVoiceContext.mOffset + response.Data.Buffer.Length;
			this.mVoiceContext.mStatus = 3;
			Log.smethod_4("NetSceneDownloadVoice", "received a msg block complete");
			if (this.mVoiceContext.mOffset == currentVoiceBlock.nTotalDataLen && currentVoiceBlock.nEndFlag == 1)
			{
				Log.smethod_4("NetSceneDownloadVoice", "received a msg complete");
				this.mVoiceContext.Dequeue();
				this.doSceneFinished(4);
				return;
			}
			if (this.mVoiceContext.mOffset != currentVoiceBlock.nTotalDataLen && currentVoiceBlock.nEndFlag == 1)
			{
				Log.smethod_5("NetSceneDownloadVoice", "large voice msg, continue download, svrID = " + this.mVoiceContext.mMsgSvrID);
				return;
			}
			this.mVoiceContext.Dequeue();
		}
	}
}
